package org.baitiaojun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.baitiaojun.pojo.CheckGroup;
import org.baitiaojun.pojo.SetMeal;
import org.baitiaojun.service.CheckGroupService;
import org.baitiaojun.service.SetMealCheckGroupService;
import org.baitiaojun.service.SetMealService;
import org.baitiaojun.util.FileNameUtil;
import org.baitiaojun.util.MessageConstant;
import org.baitiaojun.vo.PageResult;
import org.baitiaojun.vo.QueryPageBean;
import org.baitiaojun.vo.Result;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @类名 SetMealController
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/5/26 10:56
 * @版本 1.0
 */

@RestController
@RequestMapping(value = "/pages")
public class SetMealController {

    private String saveFileName;

    @Reference(interfaceClass = SetMealService.class)
    private SetMealService setMealService;

    @Reference(interfaceClass = CheckGroupService.class)
    private CheckGroupService groupService;

    @Reference(interfaceClass = SetMealCheckGroupService.class)
    private SetMealCheckGroupService setMealCheckGroupService;

    @PostMapping("/saveImage.do")
    public @ResponseBody Result saveImage(MultipartFile imgFile, HttpServletRequest request) {
        saveFileName = FileNameUtil.getUUIDFileName() + FileNameUtil.getFileType(Objects.requireNonNull(imgFile.getOriginalFilename()));
        String path = request.getServletContext().getRealPath("/img");
        File filePath = new File(path + File.separator + saveFileName);
        try {
            imgFile.transferTo(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Result result = new Result();
        result.setFlag(true);
        result.setData(saveFileName);
        return result;
    }

    @PostMapping("/saveSetMeal.do")
    public Result saveSetMeal(@RequestBody SetMeal setMeal, Integer... checkgroupIds) {
        try {
            setMeal.setImg(saveFileName);
            setMealService.insertSetMeal(setMeal, checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

    @PostMapping("/splitPageSetMealByConditions.do")
    public PageResult<SetMeal> splitPageByConditions(@RequestBody QueryPageBean queryPageBean) {
        return setMealService.splitPageByConditions(queryPageBean);
    }

    @GetMapping("/queryGroupList.do")
    public Result queryGroupList() {
        List<CheckGroup> checkGroups = groupService.queryAllCheckGroup();
        Result result = new Result();
        if (!ObjectUtils.isEmpty(checkGroups)) {
            result.setFlag(true);
            result.setData(checkGroups);
        } else {
            result.setFlag(false);
            result.setMessage(MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
        return result;
    }

    @GetMapping("/querySetMealById.do")
    public Result querySetMealById(@RequestParam Integer id) {
        SetMeal setMeal = setMealService.querySetMealById(id);
        Result result = new Result();
        if (!ObjectUtils.isEmpty(setMeal)) {
            result.setFlag(true);
            result.setData(setMeal);
        } else {
            result.setFlag(false);
        }
        return result;
    }

    @GetMapping("/queryCheckgroup.do")
    public Result queryCheckgroup() {
        List<CheckGroup> checkGroupList = groupService.queryAllCheckGroup();
        if (checkGroupList.size() > 0) {
            return new Result(true, checkGroupList);
        } else {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("/queryCheckGroupIdsBySetMealId.do")
    public Result queryCheckGroupIdsBySetMealId(@RequestParam Integer id) {
        List<Integer> ids = setMealCheckGroupService.queryCheckGroupIdsBySetMealId(id);
        return new Result(true, ids);
    }

    @PostMapping("/editSetMeal.do")
    public Result editSetMeal(Integer[] checkgroupIds, @RequestBody SetMeal setMeal) {
        try {
            setMealService.editSetMeal(checkgroupIds, setMeal);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

    @GetMapping("/removeSetMeal.do")
    public Result removeSetMeal(@RequestParam Integer id) {
        try {
            setMealService.removeSetMeal(id);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }
}
